<?php
/**
 * @title 100. 相同的树 - 递归
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {

    /**
     * @param TreeNode $p
     * @param TreeNode $q
     * @return Boolean
     */
    function isSameTree($p, $q) {
        /**
         * @since 2020-08-07 比较结果
         */
        $result = false;

        /**
         * @since 2020-08-07 null和tree判断
         */
        if($p === null AND $q === null){
            $result = true;
        } elseif($p !== null AND $q !== null) {
            /**
             * @since 2020-08-07 `p`, `q`都是tree
             * @since 2020-08-07 根节点值一致
             */
            if($p->val === $q->val){
                /**
                 * @since 2020-08-07 左子树
                 */
                if(self::isSameTree($p->left, $q->left)){
                    /**
                     * @since 2020-08-07 右子树
                     */
                    if(self::isSameTree($p->right, $q->right)){
                        $result = true;
                    } else {}
                } else {}
            } else {
                /**
                 * @since 2020-08-07 根节点值不一致
                 */
            }
        } else {
            /**
             * @since 2020-08-07 一个null, 一个tree
             */
        }

        /**
         * @return
         */
        return $result;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202008/2020.08.07.php
 */
$datas = [
    [[1,2,3], [1,2,3]],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->isSameTree($xhprof->tree($data[0]), $xhprof->tree($data[1]));
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
